#include "SerialPortController.h"

CSerialPortController::CSerialPortController()
{
	_pSerialPort = new CSerialPort();;
}

CSerialPortController::~CSerialPortController()
{
	IA_FREE(_pSerialPort);
}

int CSerialPortController::Init(int portNo, int baudRate, EParity parity, int dataBits, EStopBits stopsBits, EFlowControl flowControl, bool recvByLine)
{
	int status = _pSerialPort->Open(portNo, baudRate, parity, dataBits, stopsBits, flowControl);
	STATUS_CHECK

	status = _pSerialPort->SetParam(ESerialPortParams::IA_TAG_SERIAL_PPORT_RECEIVE_BY_LINE, recvByLine);
	STATUS_CHECK

	IA_RETURN_OK
}

int CSerialPortController::Terminate() noexcept
{
	int status = _pSerialPort->Close();
	STATUS_CHECK

	return _pSerialPort->SetSerialDataReceiver(NULL);
}

int CSerialPortController::IsOpen() noexcept
{
	return _pSerialPort->IsOpen();
}

int CSerialPortController::SetParam(ESerialPortParams paramName, int val) noexcept
{
	return _pSerialPort->SetParam(paramName, val);
}

int CSerialPortController::Flush() noexcept
{
	return _pSerialPort->Flush();
}

int CSerialPortController::WriteBytes(unsigned char* pData, int len) noexcept
{
	return _pSerialPort->WriteBytes(pData, len);
}

int CSerialPortController::WriteString(std::string text)
{
	return _pSerialPort->WriteString(text);
}

int CSerialPortController::BytesWaiting(int* pLen)
{
	return _pSerialPort->BytesWaiting(pLen);
}

int CSerialPortController::ReadBytes(unsigned char* pBuffer, int bufferSize, int bytesToRead) noexcept
{
	return _pSerialPort->ReadBytes(pBuffer, bufferSize, bytesToRead);
}

int CSerialPortController::SetSerialDataReceiver(ISerialPortReceiver* pSerialReceiver)
{
	return _pSerialPort->SetSerialDataReceiver(pSerialReceiver);
}
